package com.scalar.forecast.forecastSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.forecastSetup.forms.UserDefinedFieldsForm;
import com.scalar.forecast.forecastSetup.dao.ForecastUserFieldsDAO;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.chiptrack.db.DBTransaction;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * User: Sujan Kumar
 * Date: Feb 9, 2005
 * Time: 7:03:39 PM
 */
public class ForecastUserFieldsSaveCommand extends ActionCommand
{
      public ActionForward execute ( ActionMapping mapping,
                                            ActionForm form,
                                            HttpServletRequest request,
                                            HttpServletResponse response )
      {
          UserDefinedFieldsForm userDefinedFieldsForm = ( UserDefinedFieldsForm )form;
          HashMap errors = new HashMap ();
          HashMap info = new HashMap ();

          DBTransaction dbTransaction = null;
          boolean success = false;
          try
          {
              dbTransaction = DBTransaction.getInstance();
              dbTransaction.begin();
              success = ForecastUserFieldsDAO.saveUserDefinedFields(dbTransaction, userDefinedFieldsForm.getUserList1(), userDefinedFieldsForm.getUserDate1(), userDefinedFieldsForm.getUserText1(), userDefinedFieldsForm.getUserText2());
              if ( !success )
              {
                  errors.put ( ERROR_COULD_NOT_SAVE_USER_DEFINED_FIELDS, null );
                  request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
                  ActionForward actionForward = mapping.findForward ( "success" );
                  return actionForward;
                  //throw new Exception ( "Could Not Save the UserDefinedFields" );
              }
              success = ForecastUserFieldsDAO.saveUserDefinedFieldValues(dbTransaction, "FCUSERENTRY", userDefinedFieldsForm.getUserList1(), userDefinedFieldsForm.getUserList1Values());
              if ( !success )
              {
                  errors.put ( ERROR_COULD_NOT_SAVE_USER_DEFINED_FIELD_VALUES, null );
                  request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
                  ActionForward actionForward = mapping.findForward ( "success" );
                  return actionForward;
                  //throw new Exception ( "Could Not Save the UserDefined Field Values" );
              }
              success = true;
              info.put ( INFO_USER_DEFINED_FIELDS_SAVED_SUCCESSFULLY, null );
              request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
          }
          catch ( Exception e )
          {
              success = false;
              errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_SAVE_USER_DEFINED_FIELDS );
              request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
              e.printStackTrace();
          }
          finally
          {
              if ( dbTransaction != null )
              {
                  try
                  {
                    if ( success )
                    {
                        dbTransaction.commit();
                    }
                    else
                    {
                        dbTransaction.rollback();
                    }
                  }
                  catch( Exception e ){ }
              }
          }
          request.setAttribute ( "userDefinedFieldsForm", userDefinedFieldsForm );
          ActionForward actionForward = mapping.findForward ( "success" );
          return actionForward;
      }
}
